package com.ruoyi.blog.controller;

import com.ruoyi.blog.core.controller.BaseController;
import com.ruoyi.blog.core.domain.AjaxResult;
import com.ruoyi.blog.core.page.TableDataInfo;
import com.ruoyi.blog.domain.BlogArticles;
import com.ruoyi.blog.domain.BlogTags;
import com.ruoyi.blog.domain.vo.BlogArticleVo;
import com.ruoyi.blog.service.BlogArticlesService;
import com.ruoyi.blog.service.BlogTagsService;
import com.ruoyi.blog.utils.StringUtils;
import com.ruoyi.file.constant.FileProjectIdConstant;
import com.ruoyi.file.domain.po.FileProCol;
import com.ruoyi.file.domain.vo.FileProColVo;
import com.ruoyi.file.service.FileService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.blog.utils.DateUtils.getNowDate;

/**
 * @Author: 依梦
 * @Date: 2024/10/10
 * @Description: BlogArticlesController
 */
@RestController
@RequestMapping("/blog/articles")
public class BlogArticlesController extends BaseController {

    @Resource
    private BlogArticlesService blogArticleService;
    @Resource
    private FileService fileService;


    @GetMapping("/list")
    public TableDataInfo getList(BlogArticles blogArticles) {
        List<BlogArticleVo> list = blogArticleService.getList(blogArticles);
        list.forEach((item) -> {
            // 进行 copy 到 BlogArticlesVo 中
            List<FileProCol> fileProCols = fileService.getFileInfo(FileProjectIdConstant.PROJECT_ARTICLE, 0L, item.getArtId());
            if (StringUtils.isNotEmpty(fileProCols)) {
                item.setArtCoverUrl(fileProCols.get(0).getUrl());
            }
        });
        return getDataTable(list);
    }

    @GetMapping("/allList")
    public AjaxResult getAllList(BlogArticles blogArticles) {
        List<BlogArticleVo> list = blogArticleService.getList(blogArticles);
        return success(list);
    }


    @PostMapping("/add")
    public AjaxResult add(@RequestBody BlogArticles blogArticles) {
        blogArticles.setCreateBy(getUserId());
        blogArticles.setCreateTime(getNowDate());
        blogArticles.setUpdateBy(getUserId());
        blogArticles.setUpdateTime(getNowDate());
        return toAjax(blogArticleService.save(blogArticles));
    }

    @PutMapping("/update")
    public AjaxResult updateById(@RequestBody BlogArticles blogArticles) {
        blogArticles.setUpdateBy(getUserId());
        blogArticles.setUpdateTime(getNowDate());
        return toAjax(blogArticleService.updateById(blogArticles));
    }

    @DeleteMapping("/delete")
    public AjaxResult removeById(Long artId) {
        // TODO 还有其他条件 判断还有没有未删除的图片信息
        return toAjax(blogArticleService.removeById(artId));
    }


    @GetMapping("/artPictureById")
    public AjaxResult getArtPictureById(Long artId) {
        // 获取的是所有的文章图片信息
        List<FileProCol> fileProCols = fileService.getFileInfo(FileProjectIdConstant.PROJECT_ARTICLE, 1L, artId);
        // 获取文章信息
        BlogArticles blogArticles = blogArticleService.getById(artId);
        String artContent = blogArticles.getArtContent();
        // 封装到 blogArticleVo 中
        List<FileProColVo> fileProColVoList = new ArrayList<>();
        for (FileProCol fileProCol : fileProCols) {
            FileProColVo fileProColVo = new FileProColVo();
            BeanUtils.copyProperties(fileProCol, fileProColVo);
            fileProColVo.setIsUse(artContent.contains(fileProCol.getUrl()));
            fileProColVoList.add(fileProColVo);
        }
        return success(fileProColVoList);
    }


    @GetMapping("/getArticleById")
    public AjaxResult getArticleById(Long artId) {
        BlogArticles byId = blogArticleService.getById(artId);
        BlogArticleVo blogArticleVo = new BlogArticleVo();
        BeanUtils.copyProperties(byId, blogArticleVo);
        // 进行 copy 到 BlogArticlesVo 中
        List<FileProCol> fileProCols = fileService.getFileInfo(FileProjectIdConstant.PROJECT_ARTICLE, 0L, artId);
        blogArticleVo.setCoverFiles(fileProCols);
        return success(blogArticleVo);
    }

    /*查询最近发布的文章信息，查出前 10 篇出来*/
    @GetMapping("/recently")
    public TableDataInfo getRecentlyArticles(HttpServletRequest request) {
        /*向 request 中设置参数*/
        request.setAttribute("pageSize", 20);
        startPage();
        List<BlogArticleVo> list = blogArticleService.getRecentlyArticles();
        list.forEach((item) -> {
            // 进行 copy 到 BlogArticlesVo 中
            List<FileProCol> fileProCols = fileService.getFileInfo(FileProjectIdConstant.PROJECT_ARTICLE, 0L, item.getArtId());
            if (StringUtils.isNotEmpty(fileProCols)) {
                item.setArtCoverUrl(fileProCols.get(0).getUrl());
            }
        });
        return getDataTable(list);
    }


    /**
     * 根据文章 id 导入对应的文章信息，导出希望是 zip 的压缩包
     * @param artIds
     * @return
     */
    @PostMapping("/download")
    public AjaxResult download(Long[] artIds) {
        return blogArticleService.download(artIds);
    }

}
